package service

import (
	middleware "antiquity_map/internal/middlewares"
	repository "antiquity_map/internal/repositories"
)

type LocationService interface {
	Filter(page, pageSize, onlyBriefInfo string, request repository.Request) (repository.Result, error)
	CreateLocation(Name string, Introduction string, Latitude float64, Longitude float64) error
	DeleteLocation(locationID string) error
	GetLocationDetail(locationID string) (repository.Request_LocationDetail, error)
}

type locationService struct {
	locationRepo repository.LocationRepository
}

func NewLocationService(locationRepo repository.LocationRepository, jwtMiddleware middleware.IJWTMiddleware) LocationService {
	return &locationService{
		locationRepo: locationRepo,
	}
}

type CreateTime struct {
	EndTime   string `json:"endTime"`
	StartTime string `json:"startTime"`
}

type Location struct {
	Attitude float64 `json:"attitude"`
	Latitude float64 `json:"latitude"`
}

type UpdateTime struct {
	EndTime   string `json:"endTime"`
	StartTime string `json:"startTime"`
}
type Request struct {
	CreateTime *CreateTime `json:"createTime,omitempty"`
	Location   *Location   `json:"location,omitempty"`
	Name       *string     `json:"name,omitempty"`
	UpdateTime *UpdateTime `json:"updateTime,omitempty"`
}
type LocationInfo struct {
	CreaterCredit float64 `json:"createrCredit"`
	CreaterID     string  `json:"createrID"`
	Latitude      float64 `json:"latitude"`
	LocationID    float64 `json:"locationID"`
	LocationIntro string  `json:"locationIntro"`
	LocationName  string  `json:"locationName"`
	Longitude     float64 `json:"longitude"`
}

type Record struct {
	CreaterCredit float64 `json:"createrCredit"`
	CreaterID     string  `json:"createrID"`
	RecordID      string  `json:"recordID"`
	Thumbnail     string  `json:"thumbnail"`
	UploadTime    string  `json:"uploadTime"`
}
type Result struct {
	LocationInfo []LocationInfo `json:"locationInfo"`
	Records      []Record       `json:"records,omitempty"`
}

type Request_LocationDetail struct {
	LocationInfo []LocationInfo `json:"locationInfo"`
	Records      []Record       `json:"records,omitempty"`
}

func (s *locationService) Filter(page, pageSize, onlyBriefInfo string, request repository.Request) (repository.Result, error) {
	var result repository.Result
	// var request Request{}
	err := s.locationRepo.Filter(page, pageSize, onlyBriefInfo, request, &result)
	if err != nil {
		return result, err
	}
	return result, nil
}

func (s *locationService) CreateLocation(Name string, Introduction string, Latitude float64, Longitude float64) error {
	err := s.locationRepo.CreateLocation(Name, Introduction, Latitude, Longitude)
	if err != nil {
		return err
	}
	return nil
}

func (s *locationService) DeleteLocation(locationID string) error {
	err := s.locationRepo.DeleteLocation(locationID)
	if err != nil {
		return err
	}
	return nil
}

func (s *locationService) GetLocationDetail(locationID string) (repository.Request_LocationDetail, error) {
	var result repository.Request_LocationDetail
	result, err := s.locationRepo.GetLocationDetail(locationID)
	if err != nil {
		return result, err
	}
	return result, nil
}